package com.xixi.core.rpc.serializer;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author: zxh
 * @date: 2024/8/21 22:26
 * @description: Hessian序列化器
 */
public class CustomHessianSerializer implements CustomSerializer{

	@Override
	public <T> byte[] serialize(T object) throws IOException {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		HessianOutput output = new HessianOutput(outputStream);
		output.writeObject(object);
		return outputStream.toByteArray();
	}

	@Override
	public <T> T deserialize(byte[] bytes, Class<T> type) throws IOException {
		ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
		HessianInput input = new HessianInput(inputStream);
		return (T) input.readObject(type);
	}
}
